package com.aprilTomCat.TestTomcat;

import com.aprilTomCat.Servlet.HttpServetResponse;
import com.aprilTomCat.util.FileUtil;
import com.aprilTomCat.util.ResponseUtil;


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


public class Response implements HttpServetResponse {
    /**
     * 响应的基础信息
     */
    public static final String BASIC_RESPONSE = "HTTP/1.1 200 OK\r\n" +
            "Content-Type: text/html;charset=utf-8\r\n" +
            "Vary: Accept-Encoding\r\n";
    //创建输出流
    private OutputStream outputStream;
    public Response(OutputStream outputStream){
        this.outputStream=outputStream;
    }

    public void setSocketChannel(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public Buffer getBuffer() {
        return buffer;
    }

    public void setBuffer(ByteBuffer buffer) {
        this.buffer = buffer;
    }

    //输出通道
    private SocketChannel socketChannel;
    private ByteBuffer buffer;
    public Response(ByteBuffer buffer){
        this.buffer=buffer;
    }
    @Override
    public void getWrite(String context) throws IOException {
        System.out.println(context);
        outputStream.write(context.getBytes(StandardCharsets.UTF_8));
    }
    @Override
    public void getWriteNIO(String context) throws IOException {
        System.out.println(context);

        buffer=ByteBuffer.wrap(context.getBytes(StandardCharsets.UTF_8));
        socketChannel.write(buffer);
        //socketChannel.close();
    }
    /**
     * 返回静态资源
     * @param path
     */
    public void GetWriteHtmlNIO(String path) throws Exception {
        //资源的绝对路径
        boolean fileExist = false;
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            String filePath =  FileUtil.getResoucePath(path);
            fileExist = true;
            //读取文件内容
            byte[] bytes = Files.readAllBytes(Paths.get(filePath));

            buffer = ByteBuffer.allocate(4 * 1024);
            String context=BASIC_RESPONSE+"Server: futaoServerBaseNIO/1.1"
                    + "\r\n"+"content-length: " + bytes.length + "\r\n"+"\r\n";

            buffer.put(context.getBytes(StandardCharsets.UTF_8));
            buffer.put(bytes);
            buffer.flip();

            socketChannel.write(buffer);
        }else {
            getWriteNIO(ResponseUtil.getResponseHeader404());
        }
    }
    /**
     * 返回静态资源
     * @param path
     */
    public void GetWriteHtml(String path) throws Exception {
        //获取 resources 下边文件的路径
        String resourcesPath =  FileUtil.getResoucePath(path);
        File file = new File(resourcesPath);
        if(file.exists()){
            System.out.println("静态资源存在");
            FileUtil.writeFile(file,outputStream);
        }else {
            getWrite(ResponseUtil.getResponseHeader404());
        }
    }
}
